#include "mainwindow.h"
#include "ui_mainwindow.h"

// FILE I/O
#include <QFile>

// JSON PARSER
#include <qjson/parser.h>
#include <qjson/serializer.h>
#include <qjson/qobjecthelper.h>

// SETTINGS
#include <QSettings>

#include "professiondialog.h"

// FILE PROVIDER
#include "appfileprovider.h"

// DEBUG
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    lbCharName(0), lbCharPointTxt1(0),
    lbCharPointVal1(0), lbCharPointTxt2(0),
    lbCharPointVal2(0)
{
    ui->setupUi(this);

    // SKILL POINTS
    agSkillPoints = new QActionGroup(this);    
    agSkillPoints->addAction(ui->action16_Points)->setData(16);
    agSkillPoints->addAction(ui->action18_Points)->setData(18);
    agSkillPoints->addAction(ui->action24_Points)->setData(24);
    agSkillPoints->setExclusive(true);
    ui->action18_Points->setChecked(true);
    QAction* sep = new QAction(tr("Skill Points"), this);
    sep->setEnabled(false);
    ui->menuSetup->insertAction(ui->action16_Points, sep);

    // BUILD POINTS
    agBuildPoints = new QActionGroup(this);
    agBuildPoints->addAction(ui->action60_Points)->setData(60);
    agBuildPoints->addAction(ui->action65_Points)->setData(65);
    agBuildPoints->addAction(ui->action70_Points)->setData(70);
    agBuildPoints->setExclusive(true);
    ui->action65_Points->setChecked(true);
    sep = new QAction(tr("Build Points"), this);
    sep->setEnabled(false);
    ui->menuSetup->insertAction(ui->action60_Points, sep);

    QSettings settings;
    restoreGeometry(settings.value("geometry").toByteArray());
    restoreState(settings.value("state").toByteArray());

    parseGdrData(AppFileProvider::getConfigFile(tr("toc.json")));

    connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(onTabClosing(int)));
    connect(ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(onTabChange(int)));
}

MainWindow::~MainWindow()
{
    delete agBuildPoints;
    delete agSkillPoints;
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *)
{
    QSettings settings;
    settings.setValue("geometry", saveGeometry());
    settings.setValue("state", saveState());
}

bool MainWindow::parseGdrData(QString file)
{
    qDebug() << "Loading GDR data from: " << file;

    QFile infile(file);
    if ( !infile.open(QFile::ReadOnly) )
    {
        qCritical("Data file not found.");
        return false;
    }

    QJson::Parser parser;
    bool ok = false;

    gdrData = parser.parse(&infile, &ok).toMap();
    if (!ok)
        qCritical("An error occurred during parsing");

    infile.close();
    return ok;
}

bool MainWindow::saveGdrData(QString file)
{
    qDebug() << "Saving GDR data to: " << file;

    QFile outfile(file);
    if ( !outfile.open(QFile::WriteOnly | QFile::Truncate) )
    {
        qWarning("Cannot save data file.");
        return false;
    }

    QJson::Serializer serializer;    
    outfile.write(serializer.serialize(gdrData));
    outfile.close();
    return true;
}

void MainWindow::on_actionNew_Profession_triggered()
{
    QScopedPointer<ProfessionDialog> dlg(new ProfessionDialog(this));
    dlg->setGdrData(&gdrData);
    dlg->setWindowTitle(tr("New Profession"));
    dlg->prepareForNew();
    if ( dlg->exec() == QDialog::Accepted )
    {
        saveGdrData(AppFileProvider::getConfigFile(tr("toc.json")));
    }
}

void MainWindow::on_actionEdit_Profession_triggered()
{
    QScopedPointer<ProfessionDialog> dlg(new ProfessionDialog(this));
    dlg->setGdrData(&gdrData);
    dlg->setWindowTitle(tr("Edit Profession"));
    if ( dlg->exec() == QDialog::Accepted )
    {
        saveGdrData(AppFileProvider::getConfigFile(tr("toc.json")));
    }
}

void MainWindow::on_actionExport_PDF_Sheet_triggered()
{
    exportAsPdfSheet();
}
